package com.bw.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.common.MenuTree;
import com.bw.entity.*;
import com.bw.form.UserQueryForm;
import com.bw.mapper.SysUserMapper;
import com.bw.mapper.SysUserRoleMapper;
import com.bw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    SysUserMapper userMapper;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
    @Override
    public List<UserMenu> getAuthRouter(Long userId) {
        List<UserMenu> userMenuList = userMapper.getAuthMenu(userId);
        List<UserMenu> routerList = userMenuList.stream().filter(userMenu -> StrUtil.isNotBlank(userMenu.getPath()) && userMenu.getType() != 0).collect(Collectors.toList());
        return routerList;
    }
    @Transactional
    @Override
    public void saveUserRole(UserRoleDto userRoleDto) {
        Long userId = userRoleDto.getId();
        List<Long> list = userRoleDto.getList();
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        //删除用户之前的角色
        sysUserRoleMapper.delete(queryWrapper);
        //重新设置用户角色
        if (CollectionUtil.isNotEmpty(list)){
           list.stream().forEach(l -> sysUserRoleMapper.insert(SysUserRole.builder().roleId(l).userId(userId).build()));
        }
    }

    @Override
    public void updateUser(SysUser sysUser) {
        sysUser.setUpdateTime(new Date());
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        sysUser.setUpdateBy(principal.getUsername());
        userMapper.updateById(sysUser);
    }

    @Override
    public void addUser(SysUser sysUser) {
        String password = sysUser.getPassword();
        PasswordEncoder delegatingPasswordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
        String encode = delegatingPasswordEncoder.encode(password);
        sysUser.setPassword(encode);
        sysUser.setCreateTime(new Date());
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        sysUser.setCreateBy(principal.getUsername());
        userMapper.insert(sysUser);
    }

    @Override
    public void deleteUser(String ids) {
        userMapper.deleteUser(ids);
    }

    @Override
    public IPage<SysUser> getUserPage(Page page, UserQueryForm query) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(query.getUserName()), "user_name", query.getUserName());
        queryWrapper.like(StrUtil.isNotBlank(query.getPhonenumber()), "phonenumber", query.getPhonenumber());
        queryWrapper.eq(ObjectUtil.isNotNull(query.getDeptId()), "dept_id", query.getDeptId());
        queryWrapper.eq(StrUtil.isNotBlank(query.getStatus()), "status", query.getStatus());
        queryWrapper.eq("del_flag", 0);
        queryWrapper.le(StrUtil.isNotBlank(query.getEndDate()), "create_time", query.getEndDate());
        queryWrapper.ge(StrUtil.isNotBlank(query.getStartDate()), "create_time", query.getStartDate());
        Page selectPage = userMapper.selectPage(page, queryWrapper);

        return selectPage;
    }

    @Override
    public List<UserMenu> getAuthMenu(Long userId) {

        List<UserMenu> userMenuList = userMapper.getAuthMenu(userId);
        List<UserMenu> menuList = MenuTree.makeMenuTree(userMenuList, 0l);
        return menuList;
    }
}
